package com.briup.bookstore.service.impl;


import com.briup.bookstore.mapper.ShopCarMapper;
import com.briup.bookstore.po.Shopcar;
import com.briup.bookstore.po.ext.ShopCarExtend;
import com.briup.bookstore.service.ShopCarService;
import com.briup.bookstore.utils.BeanCopyUtils;
import com.briup.bookstore.vo.ShopCarVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

@Service
public class ShopCarServiceImpl implements ShopCarService {

    @Autowired
    ShopCarMapper shopCarMapper;

    @Override
    public List<ShopCarVO> findLists(Integer userId) {
        List<ShopCarExtend> shopcars = shopCarMapper.selectByUserId(userId);
        List<ShopCarVO> shopCarVOS = BeanCopyUtils.copyBeanList(shopcars, ShopCarVO.class);
        return shopCarVOS;
    }

    @Override
    public void add(int userId, Integer bookId) {
        // 添加购物车的时候，先判断这本书在不在购物车里
        Shopcar shopcar = shopCarMapper.selectByUserIdAndBooKId(userId, bookId);
        // 判断shopcar这个对象是否存在
        if(Objects.isNull(shopcar)){
            // 如果不在，那么就加入到购物车
            shopCarMapper.insert(userId,bookId);
        }else {
            // 如果在，就更新购物车里相同书的数量
            shopCarMapper.updateNum(shopcar.getNum()+1,shopcar.getId());
        }
    }

    @Override
    public void updateNum(Integer id, Integer num) {

    }

    @Override
    public void deleteByIds(Integer... ids) {

    }
}
